package com.analyse.stock.utils;

import org.ta4j.core.Bar;
import org.ta4j.core.BarSeries;
import org.ta4j.core.BaseBar;
import org.ta4j.core.BaseBarSeriesBuilder;
import org.ta4j.core.indicators.SMAIndicator;
import org.ta4j.core.indicators.bollinger.BollingerBandsLowerIndicator;
import org.ta4j.core.indicators.bollinger.BollingerBandsMiddleIndicator;
import org.ta4j.core.indicators.bollinger.BollingerBandsUpperIndicator;
import org.ta4j.core.indicators.helpers.ClosePriceIndicator;
import org.ta4j.core.indicators.statistics.StandardDeviationIndicator;

import java.time.Duration;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.List;

public class TrendDetection {

    public static void main(String[] args) {
        // 示例数据：每条数据包含开盘价、最高价、最低价、收盘价和成交量
        List<Bar> bars = new ArrayList<>();
        bars.add(new BaseBar(Duration.ofDays(1), ZonedDateTime.now(), 100, 105, 95, 101, 1000));
        bars.add(new BaseBar(Duration.ofDays(1), ZonedDateTime.now().plusDays(1), 101, 108, 98, 104, 1500));
        bars.add(new BaseBar(Duration.ofDays(1), ZonedDateTime.now().plusDays(2), 104, 110, 103, 107, 2000));
        bars.add(new BaseBar(Duration.ofDays(1), ZonedDateTime.now().plusDays(3), 107, 115, 106, 111, 2500));
        bars.add(new BaseBar(Duration.ofDays(1), ZonedDateTime.now().plusDays(4), 111, 120, 109, 118, 3000));
        bars.add(new BaseBar(Duration.ofDays(1), ZonedDateTime.now().plusDays(6), 107, 115, 106, 120, 2500));
        bars.add(new BaseBar(Duration.ofDays(1), ZonedDateTime.now().plusDays(7), 115, 130, 114, 130, 3000));
        // 构建时间序列
        BarSeries series = new BaseBarSeriesBuilder().withName("stock_data").withBars(bars).build();

        // 移动平均线指标：短期（5 日）和长期（10 日）
        ClosePriceIndicator closePrice = new ClosePriceIndicator(series);
        SMAIndicator shortSma = new SMAIndicator(closePrice, 5); // 短期均线
        SMAIndicator longSma = new SMAIndicator(closePrice, 10); // 长期均线

        // 判断趋势：短期均线 vs 长期均线
        int endIndex = series.getEndIndex();
        if (shortSma.getValue(endIndex).isGreaterThan(longSma.getValue(endIndex))) {
            System.out.println("当前趋势：上升趋势");
        } else if (shortSma.getValue(endIndex).isLessThan(longSma.getValue(endIndex))) {
            System.out.println("当前趋势：下降趋势");
        } else {
            System.out.println("当前趋势：横盘震荡");
        }
        // 输出 SMA 值（调试信息）
        System.out.println("短期均线 (SMA-5): " + shortSma.getValue(endIndex));
        System.out.println("长期均线 (SMA-10): " + longSma.getValue(endIndex));

        SMAIndicator sma = new SMAIndicator(closePrice, 20); // 中轨
        StandardDeviationIndicator stdDev = new StandardDeviationIndicator(closePrice, 20);
        BollingerBandsMiddleIndicator middleBand = new BollingerBandsMiddleIndicator(sma);
        BollingerBandsUpperIndicator upperBand = new BollingerBandsUpperIndicator(middleBand, stdDev);
        BollingerBandsLowerIndicator lowerBand = new BollingerBandsLowerIndicator(middleBand, stdDev);

        // 当前趋势判断
        int endIndex2 = series.getEndIndex();
        if (closePrice.getValue(endIndex2).isGreaterThan(upperBand.getValue(endIndex2))) {
            System.out.println("当前趋势：强势上升趋势");
        } else if (closePrice.getValue(endIndex2).isLessThan(lowerBand.getValue(endIndex2))) {
            System.out.println("当前趋势：强势下降趋势");
        } else {
            System.out.println("当前趋势：震荡趋势");
        }
    }
}

